home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / borland / jnfb88.zip / THINKC.ZIP / SAMPLE.C < prev   
Text File  |  1987-10-21  |  2KB  |  72 lines

  1. #define LISTSIZE  100            /* define constant */
  2. typedef int       numlist[LISTSIZE];    /* define data type */
  3.  
  4. int    lmax(numlist list, int count);    /* declare functions */
  5. void    swap(int *i, int *j);
  6. void    lsort(numlist list, int count);
  7. void    dumplist(numlist list, int count);
  8.  
  9. main()                    /* main function of program */
  10. {
  11.    numlist    list;            /* declare array */
  12.    int        count,i;        /* declare variables */
  13.  
  14.    count = 0;
  15.    do {                    /* get value from 1 to 100 */
  16.       printf("Enter # of items (1..%d):  ",LISTSIZE);
  17.       scanf("%d",&count);
  18.    } while (count < 1 && count > LISTSIZE);
  19.  
  20.    for (i=0; i<count; i++)        /* initialize array with */
  21.       list[i] = rand();            /* random values     */
  22.  
  23.    lsort(list,count);            /* sort array          */
  24.    dumplist(list,count);        /* print array on screen */
  25.    i = lmax(list,count);        /* get max value in array*/
  26.    printf("The maximum value in the list is %d\n",i);
  27.    return(0);
  28. }
  29.  
  30. int lmax(numlist list, int count)    /* return max value in array */
  31. {
  32.    int    i,max;
  33.  
  34.    max = list[0];
  35.    for (i=1; i<count; i++)
  36.       if (list[i] > max)
  37.      max = list[i];
  38.    return(max);
  39. }
  40.  
  41. void swap(int *i, int *j)        /* swap two integer values */
  42. {
  43.    int    temp;
  44.  
  45.    temp = *i; *i = *j; *j = temp;
  46. }
  47.  
  48.                    /* sort array in ascending order: */
  49. void lsort(numlist list, int count)
  50. {
  51.    int top,k,min;
  52.  
  53.    for (top=0; top < count-1; top++) {
  54.       min = top;
  55.       for (k = top+1; k < count; k++)
  56.      if (list[k] < list[min])
  57.         min = k;
  58.       if (min != top)
  59.      swap(&list[top],&list[min]);
  60.    }
  61. }
  62.  
  63. void dumplist(numlist list, int count)    /* print array on screen */
  64. {
  65.    int    i;
  66.  
  67.    for (i=0; i<count; i++)
  68.      printf("%8d",list[i]);
  69.    printf("\n");
  70. }
  71.  
  72.